package io.finer.erp.base.service.impl;

import io.finer.erp.base.entity.es.BusinessRecord;
import io.finer.erp.base.vo.DashboardRequest;
import io.finer.erp.base.vo.TrendVO;
import org.elasticsearch.action.search.SearchRequest;
import org.elasticsearch.action.search.SearchResponse;
import org.elasticsearch.index.query.BoolQueryBuilder;
import org.elasticsearch.index.query.QueryBuilders;
import org.elasticsearch.index.query.RangeQueryBuilder;
import org.elasticsearch.search.aggregations.AggregationBuilder;
import org.elasticsearch.search.aggregations.AggregationBuilders;
import org.elasticsearch.search.aggregations.Aggregations;
import org.elasticsearch.search.aggregations.bucket.histogram.DateHistogramAggregationBuilder;
import org.elasticsearch.search.aggregations.bucket.histogram.DateHistogramInterval;
import org.elasticsearch.search.aggregations.bucket.histogram.LongBounds;
import org.elasticsearch.search.aggregations.bucket.terms.TermsAggregationBuilder;
import org.elasticsearch.search.aggregations.metrics.SumAggregationBuilder;
import org.elasticsearch.search.builder.SearchSourceBuilder;
import org.jeecg.common.es.builder.BaseEsSearchBuilder;
import org.jeecg.common.es.entity.EsEntity;
import org.jeecg.common.es.entity.EsSearchContext;
import org.jeecg.common.util.DateUtils;
import org.springframework.stereotype.Component;

import java.util.Objects;

@Component
public class OrderStatisticsBuilder extends BaseEsSearchBuilder<DashboardRequest> {

    public TrendVO<Integer> orderSourceTrend(EsSearchContext<DashboardRequest> ctx) {
        SearchRequest searchRequest = buildWithoutAggregation(ctx);
        SearchSourceBuilder source = searchRequest.source();
        TermsAggregationBuilder terms = AggregationBuilders.terms("term_origin").field("origin.keyword");
        DateHistogramAggregationBuilder dateHistogram = AggregationBuilders.dateHistogram("date_historam_regtime").field("regtime").calendarInterval(getInterval(ctx.getSearchReq().getDateGranularity())).extendedBounds(getDefaultLongBounds(ctx.getSearchReq()));
        dateHistogram.subAggregation(AggregationBuilders.sum("sum_sell_count").field("sell_count"));
        terms.subAggregation(dateHistogram);
        source.aggregation(terms);
        System.out.println(source);

        SearchResponse response = esService.search(searchRequest);

        TrendVO<Integer> trendVO = new TrendVO<>();
        if (Objects.isNull(response)) {
            return buildEmptyOrderTypeTrend(ctx.getSearchReq());
        }

        return trendVO;
    }

    public static void main(String[] args) {
        OrderStatisticsBuilder orderStatisticsBuilder = new OrderStatisticsBuilder();
        DashboardRequest req = new DashboardRequest();
        req.setStartTime("2023-03-01 00:00:00");
        req.setEndTime("2023-03-12 23:59:59");
        req.setDateGranularity("DAY");
        EsSearchContext<DashboardRequest> ctx = new EsSearchContext.Builder<DashboardRequest>().buildContext(req, BusinessRecord.class);
        orderStatisticsBuilder.orderSourceTrend(ctx);
    }

    private LongBounds getDefaultLongBounds(DashboardRequest searchReq) {
        return new LongBounds(DateUtils.str2Timestamp(searchReq.getStartTime()).getTime(), DateUtils.str2Timestamp(searchReq.getEndTime()).getTime());
    }

    public TrendVO<Integer> buildEmptyOrderTypeTrend(DashboardRequest request) {
        return new TrendVO<>();
    }


    @Override
    protected SearchSourceBuilder buildSearchSourceBuilder(EsSearchContext<DashboardRequest> ctx) {
        DashboardRequest snapshotQuery = ctx.getSearchReq();
        SearchSourceBuilder searchSourceBuilder = new SearchSourceBuilder();
        BoolQueryBuilder boolQueryBuilder = QueryBuilders.boolQuery();
        RangeQueryBuilder rangeQueryBuilder = QueryBuilders.rangeQuery(EsEntity.Constants.SPIDER_TIME_FIELD);
        rangeQueryBuilder.gte(snapshotQuery.getStartTime());
        rangeQueryBuilder.lte(snapshotQuery.getEndTime());
        boolQueryBuilder.filter(rangeQueryBuilder);
        searchSourceBuilder.query(boolQueryBuilder);
        searchSourceBuilder.size(0);
        return searchSourceBuilder;
    }
}
